import requests
import json
from pyquery import PyQuery as jq
import datetime
import time
import moment
import copy


from stem import Signal
from stem.control import Controller

##代码预估价格600，数据每7000条100
url = "https://weixin.sogou.com/weixin?type=2&ie=utf8&query=%E5%8C%97%E4%BA%AC%E5%A4%A7%E5%AD%A6&tsn=5&ft=2018-10-01&et=2018-11-01&interation=&wxid=&usip=&page=1"
data = ''
mylist = []
headers = json.loads(r'''{
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Connection": "keep-alive",
    "Cookie": "ABTEST=7|1545113974|v1; IPLOC=CN3301; SUID=2AA0DC733F18960A000000005C189176; SUID=2AA0DC732C12960A000000005C189176; weixinIndexVisited=1; SNUID=35226CD9AAACD5DE4B353F52AA0C7AEB; JSESSIONID=aaaYRl5HU0df6aur_CfDw; SUV=005F1DC273C6889C5C3D6656B15DE131; sct=5",
    "Host": "weixin.sogou.com",
    "Referer": "https://weixin.sogou.com/weixin",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
}''')

class weixi():
    def __init__(self):
        self.mylist = []
        self.url = "https://weixin.sogou.com/weixin?type=2&ie=utf8&query=%E5%8C%97%E4%BA%AC%E5%A4%A7%E5%AD%A6&tsn=5&interation=&wxid=&usip="
    def start(self):
        timelist = list(self.timeSections('2016-01-01','2016-12-31',None,24 *15,'YYYY-MM-DD'))
        print(timelist)
        for times in timelist:
            for i in range(1,11):
                myurl = f'{self.url}&page={i}&ft={times[0]}&et={times[1]}'
                self.getContent(myurl)
        with open('a.json','w',encoding='utf-8') as f:
            f.write(json.dumps(self.mylist))
    def getContent(self,urls):
        resp = requests.get(urls, data=data, verify=False,headers=headers ,proxies={
            'https': 'socks5://127.0.0.1:9150',
            'http': 'socks5://127.0.0.1:9150'
        })
        resp.encoding = 'utf-8'
        print(resp.text)
        if '您的访问过于频繁' in resp.text:
            controller.signal(Signal.NEWNYM)
        else:
            for aa in jq(resp.text)('#main > div.news-box > ul > li').items():
                title = aa('.txt-box > h3').text()
                titleurl = aa('.txt-box > h3 >  a').attr('href')
                wx_gzh = aa('.txt-box > .s-p > a').text()
                times = aa('.txt-box > .s-p').attr('t')
                timeArray = time.localtime(int(times))
                timetxt = time.strftime("%Y-%m-%d", timeArray)
                self.mylist.append({"title":title,"url":titleurl,"gxh":wx_gzh,"time":times,"timetxt":timetxt})


    def timeSections(self,starttimes, endtimes, sectiondays=30, sectionshours=1, formats=None):
        if not starttimes:
            starttimes = moment.now().replace(minutes=0, seconds=0).add(days=-sectiondays)
        else:
            starttimes = moment.date(starttimes)
        if not endtimes:
            endtimes = moment.now().replace(minutes=0, seconds=0)
        else:
            endtimes = moment.date(endtimes)

        while starttimes < endtimes:
            nexttimes = copy.deepcopy(starttimes).add(hours=sectionshours)
            if formats:
                yield starttimes.format(formats), nexttimes.format(formats)
            else:
                yield starttimes, nexttimes
            starttimes = copy.deepcopy(nexttimes).add(hours=24)
if __name__ == '__main__':
    controller = Controller.from_port(port=9151)
    controller.authenticate()
    weixi().start()